//
//  CEHttper.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-12-13.
//

import Foundation

class CEHttper: CJHttper {
    
    func login(userName: String, password: String,
               didCompleted: @escaping CJHttpModelClosure<CERootModel<CELoginModel>>) {
        let param = ["userName": userName, "password": password]
        let point = "/doctor/user/login"
        
        httper.httpModel(point: point, params: param,
                         modelType: CERootModel<CELoginModel>.self) { config, model in
            if config.state == .finish {
                let localCenter = CJLocalCenter<CELoginModel>.shared()
                localCenter.localModel = model?.data
                localCenter.localModel?.logined = true
                localCenter.save()
            }
            didCompleted(config, model)
        }
    }
    
    func userInfo(didCompleted: @escaping CJHttpModelClosure<CERootModel<CEUserInfoModel>>) {
        let point = "/doctor/user/info"
        httper.httpModel(method: .get, point: point, modelType: CERootModel<CEUserInfoModel>.self) { config, model in
            if config.state == .finish, let userInfo = model?.data {
                let localCenter = CJLocalCenter<CELoginModel>.shared()
                localCenter.localModel?.userInfo = userInfo
                localCenter.save()
            }
            didCompleted(config, model);
        }
    }
    
}

extension CEHttper {
    
    func refreshHttp(config: CJHttpConfig, didCompleted: @escaping CJHttpClosure) {
        let owner: [CJHttpOwnerKeys: Any] = [.noLockKey: "0", .refreshHeadersKey: "1"]
        config.owners = config.owners.merging(owner, uniquingKeysWith: { _, n in
            n
        })
        httper.httpHandel(config: config, didCompleted: didCompleted)
    }
}

extension CEHttper {
    
    func refreshTest(didCompleted: @escaping CJHttpClosure) {
        let localCenter = CJLocalCenter<CELoginModel>.shared()
        let param = ["refreshToken": (localCenter.localModel?.token.refreshToken ?? "")]
        let point = "/doctor/user/login/refresh-token"
        
        httper.httpModel(point: point, params: param,
                         modelType: CERootModel<CETokenModel>.self) { config, model in
            if config.state == .finish, let refreshToken = model?.data?.refreshToken {
                localCenter.localModel?.token.refreshToken = refreshToken
                localCenter.save()
            }
            didCompleted(config)
        }
    }
    
    func loginTest(userName: String, password: String,
                   didCompleted: @escaping CJHttpClosure) {
        let param = ["userName": userName, "password": password]
        let point = "/doctor/user/login"
        
        httper.httpModel(point: point, params: param,
                         modelType: CELoginModel.self, decodPaths: ["data"]) { config, model in
            if config.state == .finish {
                let localCenter = CJLocalCenter<CELoginModel>.shared()
                localCenter.localModel = model
                localCenter.localModel?.logined = true
                localCenter.save()
            }
        }
    }
    
    func refreshToken(didCompleted: @escaping CJHttpClosure) {
        let localCenter = CJLocalCenter<CELoginModel>.shared()
        let param = ["refreshToken": (localCenter.localModel?.token.refreshToken ?? "")]
        let point = "/doctor/user/login/refresh-token"
        let owner: [CJHttpOwnerKeys: Any] = [.noLockKey : "1"]
        
        httper.httpModel(point: point, params: param, owner: owner,
                         modelType: CERootModel<CETokenModel>.self) { config, model in
            if config.state == .finish, let token = model?.data {
                localCenter.localModel?.token = token
                localCenter.save()
            }
            didCompleted(config)
        }
    }
}

extension CEHttper {
    
    func down() {
        let url = "http://35.158.110.164:8001/VBox50_8850_V0028_To_V0029.pac"
        let config = CJHttpConfig.config(method: .get, httpMethod: .download, url: url)
        config.downPolicy = .caches
        httper.httpHandel(config: config) { config in
            
            kCJLog(config)
            
        }
    }
    
    
}
